package by.demchenko.sample.webapp.page.home.panel;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.enums.UserType;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.PersonService;
import by.demchenko.sample.webapp.page.home.cards.CourseCard;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;

import javax.inject.Inject;
import java.util.*;

public class SidebarPanel extends Panel {

    @Inject
    PersonService personService;

    @Inject
    CourseService courseService;

    public SidebarPanel(String id) {
        super(id);

    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        final List<Person> teachers = personService.getPersonsByType(UserType.TEACHER, false);
        Random rand = new Random();
        while (teachers.size() > 4) {
            int randomNum = rand.nextInt((teachers.size() - 1) + 1);
            teachers.remove(randomNum);
        }
        add(new ListView<Person>("details-panel", teachers) {
            @Override
            protected void populateItem(ListItem<Person> item) {
                Person teacher = item.getModelObject();
                item.add((new TeacherPanel("teacher-panel", teacher)));
            }
        });

        Form<Void> form = new Form<>("searchform");
        add(form);

        List<Course> courses = courseService.getAll();

        final AutoCompleteTextField<Course> ebSearch = new AutoCompleteTextField<Course>("editbox_search", new Model<Course>()) {

            @Override
            protected Iterator<Course> getChoices(String input) {
                if ((Strings.isEmpty(input)) ||(input.length() < 3)) {
                    List<Course> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<Course> choises = new ArrayList<>(5);

                for (final Course course : courses) {
                    if (course.getName().toUpperCase().contains(input.toUpperCase())) {
                        choises.add(course);
                        if (choises.size() == 5) {
                            break;
                        }
                    }
                }

                return choises.iterator();

            }
        };

        form.add(ebSearch);
        ebSearch.add(new AttributeModifier("placeholder", getString("label.find")));

        ebSearch.add(new AjaxFormSubmitBehavior(form, "onchange") {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                String findString = ((AutoCompleteTextField) this.getComponent()).getInput();
                Course findedCourse = courseService.getByName(findString);
                if (findedCourse != null) {
                    setResponsePage(new CourseCard(findedCourse));
                }
            }

            @Override
            protected void onError(AjaxRequestTarget target) {

            }
        });
    }
}
